/**
 * 销售回款预测
 * @author liuw
 * @date 2019/09/02
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'YEAR_', width: 80, title: '年度', fixed: 'left',sort: true}
            , {field: 'MONTH_', width: 80, title: '期别', fixed: 'left',search: 'default',
                searchOpts: {type: 'select', default: ''},
                filterDatas: app.datas_('crm_gwat550_qb')
            }
            , {field: 'GWAKL003', width: 140, title: '客户名称', fixed: 'left'}
            , {field: 'GWAKL003_', width: 140, title: '经销商全称'}
            , {field: 'GWBS003', width: 160, title: '品号'}
            , {field: 'GWAML003', width: 180, title: '品名'}
            , {field: 'GWAML004', width: 160, title: '规格'}
            , {field: 'GWABL003', width: 160, title: '单位'}
            , {field: 'GWBS005', width: 80, title: '数量'}
            , {field: 'GWBS006', width: 80, title: '单价'}
            , {field: 'GWBS007', width: 80, title: '金额'}
            , {field: 'GWBS012', width: 100, title: '回款年度',edit:'text',search: 'default',
                searchOpts: {type: 'year'}, sort: true
            }
            , {field: 'GWBS013', width: 100, title: '回款月份',edit:'text'}
            , {field: 'GWBS014', width: 100, title: '回款周',edit:'text'}
            , {field: 'GWBS008', width: 100, title: '成交几率'}
            , {field: 'GWBS019', width: 140, title: '已回款金额',edit:'text'}
            , {field: 'GWBS020', width: 100, title: '回款日期',edit:'date'}
            , {field: 'GWBS018_NAME_', width: 140, title: '大区/分公司'}
            , {field: 'GWAOL003', width: 140, title: '产品系列'}
            , {field: 'GWADL004', width: 100, title: '省份'}
            , {field: 'GWAGL003', width: 100, title: '行业'}
            , {field: 'GWBS010_NAME_', width: 100, title: '业务人员',
                search: 'default', searchOpts: {icon: true}
            }
            , {field: 'GWBS022_NAME_', width: 100, title: '业务部门'}
            , {field: 'GWBS021', width: 100, title: '备注'}
            , {field: 'GWBSNAME', width: 180, title: '预测单号'}
            , {field: 'GWBSSEQ', width: 80, title: '项次'}
            , {field: 'GWBSDOCDATE', width: 100, title: '单据日期'}
            , {field: 'GWBSACTI', width: 80, title: '有效否',filterDatas: app.datas_('sys_y_n')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwat550setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwat550SettingAction,
        gwatTable,
        moduleName = '销售回款预测'
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwat550',
                _searchs: [{
                    id: 'gwat550', tableId: 'gwat550_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwat550_edit', tpl: 'gwat550_edit'
                },{
                    dom: 'gwat550_auto', tpl: 'gwat550_auto'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwatTable = table.render({
                    elem: app.getByid(doc, 'gwat550_table'),
                    url: g_ctxPath + 'crm/gwat550/getGwbslist',
                    exportsUrl: g_ctxPath + 'crm/gwat550/getGwbslist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwat550_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-gwat550setting-action').innerHTML).render({}, function (string) {
                    gwat550SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwat550_table)', function (obj) {

                    layer.tips(gwat550SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwat550_edit',
                                            tagData: {
                                                moduleName:moduleName,
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                minmax: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwat550_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwat550_edit',
                        tagData: {
                            moduleName:moduleName,
                            obj:obj
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '信息编辑',
                            minmax: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwat550_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwat550_edit',
                                tagData: {
                                    moduleName:moduleName,
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    minmax: true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'confirm':
                            self.confirm();
                            break;
                        case 'confirmCancel':
                            self.confirmCancel();
                            break;
                        case 'autoGeneration':
                            self.autoGeneration();
                            break;
                        case 'saveTable':
                            self.saveTable();
                            break;
                    }
                });

                table.on('edit(gwat550_table)', function (obj) {
                    if (obj.field == 'GWBS012') {

                    }else if(obj.field == 'GWBS013'){

                    }else if(obj.field == 'GWBS014'){

                    }else if(obj.field == 'GWBS019'){

                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwat550_table', where: table_where});
        },

        delData: function (obj) {
            app.ajax({
                xurl: 'crm/gwat550/del',
                xdata: obj.data,
                loading: true,
                confirm: {msg: "是否删除选择的数据？"},
                okmsg: moduleName + '删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwat550_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWBS_T');
                    _temp.GWBSENT = v.GWBSENT;
                    _temp.GWBSNAME = v.GWBSNAME;
                    _temp.GWBSSEQ = v.GWBSSEQ;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwat550/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否删除选择的数据？"},
                    okmsg: moduleName + '信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        confirm: function(){
            var flag = true;
            var checkStatus = table.checkStatus('gwat550_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if(v.GWBSACTI === 'Y'){
                        app.showMessage("存在已确认的数据，不可重复确认！");
                        flag = false;
                        return;
                    }
                    var _temp = app.fields_('GWBS_T');
                    _temp.GWBSENT = v.GWBSENT;
                    _temp.GWBSNAME = v.GWBSNAME;
                    _temp.GWBSSEQ = v.GWBSSEQ;
                    _temp.GWBSACTI = 'Y';
                    _xdata.push(_temp);
                });
            }else {
                app.showMessage("请先选择数据！");
                return ;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwat550/updateDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否确认选择的数据？"},
                    okmsg: moduleName + '信息确认成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        confirmCancel: function(){
            var flag = true;
            var checkStatus = table.checkStatus('gwat550_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if(v.GWBSACTI == 'N'){
                        app.showMessage("存在未确认的数据，不可取消确认！");
                        flag = false;
                        return;
                    }
                    var _temp = app.fields_('GWBS_T');
                    _temp.GWBSENT = v.GWBSENT;
                    _temp.GWBSNAME = v.GWBSNAME;
                    _temp.GWBSSEQ = v.GWBSSEQ;
                    _temp.GWBSACTI = 'N';
                    _xdata.push(_temp);
                });
            }else {
                app.showMessage("请先选择数据！");
                return ;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwat550/updateDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否取消确认选择的数据？"},
                    okmsg: moduleName + '信息取消确认成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        autoGeneration: function(){
            self._openTplFull({
                tag: 'gwat550_auto',
                tagData: {

                },
                index: 1,
                openOpts: {
                    title: '自动生成',
                    area: ['500px', '300px']
                }
            });
        },

        saveTable:function () {
            var list = gwatTable.getDatas();
            app.ajax({
                xurl: 'crm/gwat550/updateDataBatch',
                xdata: list,
                loading: true,
                toJsonStr: true,
                okmsg: '回款信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwat550',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWBS010_NAME_') {//业务人员开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '业务员编号', prop: 'ID', width: '150'},
                                                {label: '业务员名称', prop: 'NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWBS010_NAME_ = data.ACCOUNT;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择业务员',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});